package com.yanqu.road.server.cmd.beautyescort;

import com.yanqu.road.entity.enums.eItemId;
import com.yanqu.road.entity.player.UserInfo;
import com.yanqu.road.entity.union.cross.CrossUnionMember;
import com.yanqu.road.pb.beautyescort.BeautyEscortProto;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.beautyescort.BeautyEscortModule;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.gameplayer.module.player.PreReduceModule;
import com.yanqu.road.server.manger.beautyescort.BeautyEscortMgr;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.manger.union.CrossUnionMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;
import com.yanqu.road.utils.string.StringUtils;

import java.util.Map;
import java.util.Set;

@Cmd(code = Protocol.S_BEAUTY_ESCORT_SEARCH_USER, desc = "查探")
public class BeautyEscortSearchCmd extends AbstractCommand {


    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        BeautyEscortProto.BeautyEscortSearchUserReqMsg reqMsg = BeautyEscortProto.BeautyEscortSearchUserReqMsg.parseFrom(packet.getBytes());
        int groupId = BeautyEscortMgr.getGroupId(GameServer.getInstance().getServerId());
        Set<Long> serverIdSet = BeautyEscortMgr.getGroupServer(groupId);
        if(!serverIdSet.contains(reqMsg.getServerId())){
            ret = GameErrorCode.E_BEAUTY_ESCORT_USER_NOT_IN_GROUP;
            return;
        }
        if(StringUtils.isNullOrEmpty(player.getUserInfo().getUnionUid())){
            ret = GameErrorCode.E_UNION_NO_JOIN_UNION;
            return;
        }
        //自己或者商会成员判定
        if(GameServer.getInstance().getServerId() == reqMsg.getServerId()){
            if(player.getUserInfo().getUserAliasId() == reqMsg.getUserAliasId()){
                ret = GameErrorCode.E_BEAUTY_ESCORT_CAN_NOT_SEARCH_SELF;
                return;
            }
            Map<Long, CrossUnionMember> memberMap = CrossUnionMgr.getUnionMemberMap(player.getUserInfo().getUnionUid());
            if(memberMap != null){
                for (Map.Entry<Long, CrossUnionMember> memberEntry : memberMap.entrySet()) {
                    UserInfo userInfo = UserMgr.getUserInfo(memberEntry.getKey());
                    if(userInfo != null){
                        if(userInfo.getUserAliasId() == reqMsg.getUserAliasId()){
                            ret = GameErrorCode.E_BEAUTY_ESCORT_CAN_NOT_SEARCH_SAME_UNION;
                            return;
                        }
                    }
                }
            }
        }
        boolean groupOpen = player.getModule(BeautyEscortModule.class).systemOpen();
        if(groupOpen){
            //消耗
            String searchCost = eItemId.BEAUTY_ESCORT_MAGNIFIER.getValue() + "=" + 1;
            Property consume = PropertyHelper.parseStringToProperty(searchCost);
            //预扣除，如果对方未在走镖，无需扣除
            if(!player.getModule(CurrencyModule.class).currencyIsEnough(consume)){
                ret = GameErrorCode.E_BAG_ITEM_NO_ENOUGH;
                return;
            }
            player.getModule(PreReduceModule.class).preReduce(searchCost);
            player.sendPacket(Protocol.C_BEAUTY_ESCORT_SEARCH_USER, reqMsg.toBuilder());
        }else {
            ret = GameErrorCode.E_BEAUTY_ESCORT_SYSTEM_NO_OPEN;
        }
    }

    @Override
    public void sendData(GamePlayer player) {
        if(ret != 0){
            BeautyEscortProto.BeautyEscortSearchUserRespMsg.Builder respMsg = BeautyEscortProto.BeautyEscortSearchUserRespMsg.newBuilder();
            respMsg.setRet(ret);
            player.sendPacket(Protocol.U_BEAUTY_ESCORT_SEARCH_USER, respMsg);
        }
    }
}
